package com.codoon.unova.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import android.annotation.SuppressLint;

/**
 * 获取后面N天的时间类
 * 
 * @author Administrator
 * 
 */
public class DateUtil {

	@SuppressLint("SimpleDateFormat")
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	public static long dateMills = 24 * 60 * 60 * 1000;// 一天对应的毫秒数
	private static Calendar c;

	/**
	 * 获得当天之后的N天日期
	 * 
	 * @return
	 */
	public static List<String> getNextDays(String currDate, int num) {
		List<String> list = new ArrayList<String>();
		list.add(currDate);

		c = Calendar.getInstance();

		try {
			c.setTime(sdf.parse(currDate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < num; i++) {
			list.add(sdf.format(getAfterDate(1)));
		}
		return list;
	}

	/**
	 * 字符串转换到时间格式
	 * 
	 * @param dateStr
	 *            需要转换的字符串
	 * @param formatStr
	 *            需要格式的目标字符串 举例 yyyy-MM-dd
	 * @return Date 返回转换后的时间
	 * @throws ParseException
	 *             转换异常
	 */
	public static Date StringToDate(String dateStr, String formatStr) {
		DateFormat sdf1;
		if (formatStr != null && !formatStr.equals("")) {
			sdf1 = new SimpleDateFormat(formatStr);
		} else {
			sdf1 = sdf;
		}
		Date date = null;
		try {
			date = sdf1.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 时间格式转换到字符串
	 * 
	 * @param date
	 *            需要转换的日期
	 * @return String 返回转换后的时间
	 */

	public static String DateToString(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		return df.format(date);
	}

	public static String DateTimeToString(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return df.format(date);
	}

	public static String DateTimeToString(String str_date) {

		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date date = df.parse(str_date);
			df = new SimpleDateFormat("d");
			return df.format(date);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;

	}

	public static String DateChangeFromat(String strDate) {
		DateFormat df = new SimpleDateFormat("yyyy年MM月");
		Date date;
		try {
			date = sdf.parse(strDate);
			return df.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return strDate;

	}

	public static String DateTime_ss_ToString(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	/**
	 * 获取当前日期n天后的日期
	 * 
	 * @param n
	 *            :返回当天后的第N天
	 * @return 返回的日期
	 */
	public static Date getAfterDate(int n) {

		c.add(Calendar.DAY_OF_MONTH, n);
		return c.getTime();
	}

	/**
	 * 获取当前日期n天后的日期
	 * 
	 * @param n
	 *            :返回当天后的第N天
	 * @return 返回的日期
	 */
	public static Date getDate(int n) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, n);
		return c.getTime();
	}

	public static String fmtDateToStr(Date date, String dtFormat) {
		if (date == null)
			return "";
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
			return dateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public static Date fmtStrToDate(String date, String dtFormat) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
			return dateFormat.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获得指定日期的前一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = sdf.parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = sdf.format(c.getTime());
		return dayBefore;
	}

	/**
	 * 获得指定日期的后一天
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = sdf.parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = sdf.format(c.getTime());
		return dayAfter;
	}

	/**
	 * 获取当前日期n天后的日期
	 * 
	 * @param n
	 *            :返回当天后的第N天
	 * @return 返回的日期
	 */
	public static Date addDay(Date date1, int days) {
		Calendar date = Calendar.getInstance();
		date.setTime(date1);
		date.add(Calendar.DAY_OF_YEAR, days);
		return date.getTime();
	}

	/** * 获取日期年份 * @param date 日期 * @return */
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/** * 功能描述：返回月 * * @param date * Date 日期 * @return 返回月份 */
	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH);
	}

	/** * 功能描述：返回日 * * @param date * Date 日期 * @return 返回日份 */
	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 可以获取n天前或n天后的日期
	 * 
	 * @param dateStr
	 * @param day
	 * @return
	 * @throws ParseException
	 */
	public static String addDate(String dateStr, long num) {
		Date d = StringToDate(dateStr, null);
		long time = d.getTime();
		long day = num * dateMills;
		time += day;
		String dStr = sdf.format(new Date(time));
		return dStr;

	}

	public static final int daysBetween(String early, String late) {
		Date earlydate = new Date();
		Date latedate = new Date();
		try {
			earlydate = sdf.parse(early.trim());
			latedate = sdf.parse(late.trim());
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		java.util.Calendar calst = java.util.Calendar.getInstance();
		java.util.Calendar caled = java.util.Calendar.getInstance();
		calst.setTime(earlydate);
		caled.setTime(latedate);
		// 设置时间为0时
		calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
		calst.set(java.util.Calendar.MINUTE, 0);
		calst.set(java.util.Calendar.SECOND, 0);
		caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
		caled.set(java.util.Calendar.MINUTE, 0);
		caled.set(java.util.Calendar.SECOND, 0);
		// 得到两个日期相差的天数
		int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
		if (days == 0) {
			days = 1;
		}
		return days;
	}

	/**
	 * 日期转化为毫秒数
	 * 
	 * @param dateStr
	 * @return
	 */
	public static long dateToMills(String dateStr) {
		// TODO Auto-generated method stub
		Calendar c = Calendar.getInstance();

		try {
			c.setTime(sdf.parse(dateStr));
			return c.getTimeInMillis();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * 根据日期取出是星期几,数字
	 * 
	 * @param date
	 * @return int 返回1-7
	 */
	public static int getWeekOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return (calendar.get(Calendar.DAY_OF_WEEK) - 1) == 0 ? 7 : calendar.get(Calendar.DAY_OF_WEEK) - 1;
	}

	/**
	 * 根据日期 找到对应日期的 星期
	 */
	public static int getDayOfWeekByDate(String date) {
		int dayOfweek = 0;
		try {
			SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
			Date myDate = myFormatter.parse(date);
			dayOfweek = getWeekOfDate(myDate);

		} catch (Exception e) {
			System.out.println("错误!");
		}
		return dayOfweek;
	}

	/**
	 * 
	 * <p>
	 * function :判断日期大小
	 * </p>
	 * 
	 * @param startDate
	 *            ：开始日期
	 * @param endDate
	 *            ：结束日期
	 * @param fromat
	 *            ：日期格式
	 * @return :如果开始时间大于结束时间则返回true，反之则返回false
	 */
	public static boolean compareToCalendar(String startDate, String endDate, String fromat) {
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		startCalendar.setTime(fmtStrToDate(startDate, fromat));
		endCalendar.setTime(fmtStrToDate(endDate, fromat));
		/* 如果开始时间大于结束时间则返回true */
		if (startCalendar.compareTo(endCalendar) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * <p>
	 * function :计算两个日期差
	 * </p>
	 * 
	 * @param startDate
	 * @param endDate
	 * @param fromat
	 * @return :
	 */
	public static int daysBetween(String startDate, String endDate, String fromat) {
		SimpleDateFormat sdf = new SimpleDateFormat(fromat);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(startDate));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(endDate));
			long time2 = cal.getTimeInMillis();
			long between_days = (time2 - time1) / (1000 * 3600 * 24);
			return Integer.parseInt(String.valueOf(between_days));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return 1;
	}

	/**
	 * 
	 * <p>
	 * function :根据输入的时间， 计算days天后的日期
	 * </p>
	 * 
	 * @param startStr
	 * @param days
	 * @return :
	 */
	public static String calculateDaysAfterDate(String startStr, int days) {
		Calendar date = Calendar.getInstance();
		try {
			date.setTime(sdf.parse(startStr));
			date.add(Calendar.DAY_OF_YEAR, days);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return sdf.format(date.getTime());
	}

	/**
	 * 
	 * Function:两个时间差是否在指定的范围内
	 * 
	 * @param startDate
	 * @param endDate
	 * @param format
	 * @param min
	 *            :相差的分钟数
	 * @return
	 */
	public static boolean incluedDateInterval(String startDate, String endDate, String format, int min) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		try {
			Date date1 = df.parse(startDate);
			Date date2 = df.parse(endDate);
			// 如果小于10分钟，则不能插入数据库
			if (date2.getTime() - date1.getTime() > (1000 * 60 * min)) {
				return false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return true;
	}

	public static boolean incluedDateInterval(String startDate, Date endDate, String format, int min) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		try {
			Date date = df.parse(startDate);
			// 如果小于10分钟，则不能插入数据库
			if (endDate.getTime() - date.getTime() > (1000 * 60 * min)) {
				return true;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

	/**
	 * 
	 * Function: 获取当前时间
	 * 
	 * @return
	 */
	private static long getCurrentTime() {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		format.setTimeZone(TimeZone.getDefault());
		return getMinTime(format.format(new Date(System.currentTimeMillis())));
	}

	/**
	 * 
	 * Function: 获取分钟
	 * 
	 * @param time
	 * @return
	 */
	private static long getMinTime(String time) {
		if (!time.contains(":")) {
			return -1;
		}
		String[] arrTime = time.split(":");
		long lTime = (Integer.valueOf(arrTime[0]) * 60 * 60 * 1000) + (Integer.valueOf(arrTime[1]) * 60 * 1000);
		return lTime;
	}

	/**
	 * 
	 * Function: 获取小时
	 * 
	 * @param hour
	 * @return
	 */
	private static long getHourTime(String hour) {
		if (hour.contains(":")) {
			int ihour = Integer.valueOf(hour.split(":")[0]);
			int iMin = Integer.valueOf(hour.split(":")[1]);
			long lTime = (Integer.valueOf(ihour) * 60 * 60 * 1000) + (iMin * 60 * 1000);
			return lTime;
		} else {
			long lTime = (Integer.valueOf(hour) * 60 * 60 * 1000);
			return lTime;
		}
	}
	/****************************活动提醒中计算当前时间是否在用户设置的时间范围之内***END****************************************************/
}